import { Color, Mark } from "./enum";
import { Card, Deck, Jocker, NormalCard } from "./type";

export function createDeck(): Deck {
  const deck: Deck = [];

  const marks = Object.values(Mark); // Object.values()会运行才能知道marks，而在运行过程中ts会被删除，因此此时这里为any类型
  const colors = Object.values(Color);

  for (const m of marks) {
    for (const c of colors) {
      // ts 对象兼容性
      // const card: NormalCard = {
      //   mark: m,
      //   color: c,
      //   getString: () => {
      //     return this.card + this.mark;
      //   },
      // };
      // deck.push(card);

      // ts 类型断言
      deck.push(<Card>{
        mark: m,
        color: c,
        getString() {
          return this.color + this.mark;
        },
      });
    }
  }

  const jo: Jocker = {
    type: "small",
    getString() {
      return "jo";
    },
  };

  deck.push(jo);
  const jocker: Jocker = {
    type: "big",
    getString() {
      return "jocker";
    },
  };

  deck.push(jocker);
  return deck;
}

export function printDeck(deck: Deck) {
  let result = "\n";
  deck.forEach((card, i) => {
    result += card.getString() + "\t";
    if ((i + 1) % 6 === 0) {
      result += "\n";
    }
  });
  console.log(result);
}

export {};
